"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.WebLayout = void 0;

var _react = _interopRequireWildcard(require("react"));

var _configContext = require("../../../extends/configContext");

var _icon = _interopRequireDefault(require("../../icon"));

var _classnames = _interopRequireDefault(require("classnames"));

var _excluded = ["className", "style", "value", "icon", "prefixIcon", "mode", "placeholder", "disabled", "maxLength", "showLimit", "clearable", "append", "appendWidth"];

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var WebLayout = /*#__PURE__*/function (_Component) {
  _inherits(WebLayout, _Component);

  var _super = _createSuper(WebLayout);

  function WebLayout(props) {
    var _this;

    _classCallCheck(this, WebLayout);

    _this = _super.call(this, props);

    _defineProperty(_assertThisInitialized(_this), "oldValue", void 0);

    _defineProperty(_assertThisInitialized(_this), "inputLock", void 0);

    _defineProperty(_assertThisInitialized(_this), "onCompositionStart", function (event) {
      _this.inputLock = true;
    });

    _defineProperty(_assertThisInitialized(_this), "onCompositionEnd", function (event) {
      _this.inputLock = false;

      _this.onInput(event);
    });

    _this.state = {
      type: props.mode === 'password' ? 'password' : props.htmlType,
      isFocus: _this.props.focus
    };
    _this.inputLock = false;
    _this.oldValue = '';
    return _this;
  }

  _createClass(WebLayout, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      if (this.props.focus) {
        var inputElem = this.refs.input;
        inputElem && typeof inputElem.focus === 'function' && inputElem.focus();
      }

      if (this.props.useRef) {
        var _inputElem = this.refs.input;
        this.props.useRef(_inputElem);
      }
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      if (!this.props.value) {
        this.refs.input.value = '';
      }
    }
  }, {
    key: "onIconClick",
    value: function onIconClick(event) {
      if (this.props.disabled) return;
      typeof this.props.onIconClick === 'function' && this.props.onIconClick(event);
    }
  }, {
    key: "onPrefixIconClick",
    value: function onPrefixIconClick(event) {
      if (this.props.disabled) return;
      typeof this.props.onPrefixIconClick === 'function' && this.props.onPrefixIconClick(event);
    }
  }, {
    key: "onClick",
    value: function onClick(event) {
      typeof this.props.onClick === 'function' && this.props.onClick(event);
    }
  }, {
    key: "onInput",
    value: function onInput(event) {
      if (this.props.disabled) {
        typeof this.props.onChange === 'function' && this.props.onChange(event);
        return this.props.value;
      }

      if (this.inputLock) {
        typeof this.props.onChange === 'function' && this.props.onChange(event);
        return this.props.value;
      }

      if (this.props.trim) {
        event.target.value = event.target.value.replace(/\s+/g, '');
      } // 这里根据mode来判断，只容许数字输入


      if (this.props.mode === 'number') {
        var str = event.target.value;
        event.target.value = str.replace(/[^\d+|.]/g, '');
      }

      typeof this.props.onChange === 'function' && this.props.onChange(event);
    }
  }, {
    key: "onFocus",
    value: function onFocus(event) {
      this.setState({
        isFocus: true
      });
      typeof this.props.onFocus === 'function' && this.props.onFocus(event);
    }
  }, {
    key: "onBlur",
    value: function onBlur(event) {
      // 数字类型格式化输入的内容
      if (this.props.mode === 'number') {
        var str = event.target.value;

        if (str === '') {
          event.target.value = '';
        } else if (isNaN(Number(str))) {
          event.target.value = this.oldValue;
        } else {
          event.target.value = String(Number(event.target.value));
        }

        this.oldValue = event.target.value;
        typeof this.props.onChange === 'function' && this.props.onChange(event);
      }

      typeof this.props.onBlur === 'function' && this.props.onBlur(event);
      this.setState({
        isFocus: false
      });
    }
  }, {
    key: "onKeyDown",
    value: function onKeyDown(event) {
      if (this.props.disabled) return;

      if (event.keyCode === 13) {
        typeof this.props.onEnter === 'function' && this.props.onEnter(event);
      }

      if (event.keyCode === 8) {
        typeof this.props.onBackspace === 'function' && this.props.onBackspace(event);
      }
    }
  }, {
    key: "onClearable",
    value: function onClearable(event) {
      if (this.props.disabled) return;

      if (typeof this.props.onChange === 'function') {
        event.target.value = '';
        this.props.onChange(event);
      }
    }
  }, {
    key: "onPassword",
    value: function onPassword() {
      this.setState(function (state, props) {
        return {
          type: state.type === 'password' ? props.htmlType : 'password'
        };
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props = this.props,
          className = _this$props.className,
          style = _this$props.style,
          _this$props$value = _this$props.value,
          value = _this$props$value === void 0 ? this.props.defaultValue || '' : _this$props$value,
          _this$props$icon = _this$props.icon,
          icon = _this$props$icon === void 0 ? '' : _this$props$icon,
          _this$props$prefixIco = _this$props.prefixIcon,
          prefixIcon = _this$props$prefixIco === void 0 ? '' : _this$props$prefixIco,
          mode = _this$props.mode,
          placeholder = _this$props.placeholder,
          disabled = _this$props.disabled,
          maxLength = _this$props.maxLength,
          showLimit = _this$props.showLimit,
          clearable = _this$props.clearable,
          append = _this$props.append,
          _this$props$appendWid = _this$props.appendWidth,
          appendWidth = _this$props$appendWid === void 0 ? '50px' : _this$props$appendWid,
          webProps = _objectWithoutProperties(_this$props, _excluded);

      var clsPrefix = this.context.clsPrefix;
      var componentPrefix = "".concat(clsPrefix, "-input");
      var componentClassNames = (0, _classnames.default)(componentPrefix, className, {
        'is-disabled': disabled,
        'is-focus': this.state.isFocus
      });
      var passwordClassNames = (0, _classnames.default)("".concat(componentPrefix, "__icon"), "".concat(componentPrefix, "__select-icon"), {
        'is-select': this.state.type !== 'password'
      }); // 内置常见前景模式

      var modeNode;

      if (mode && mode === 'password') {
        modeNode = /*#__PURE__*/_react.default.createElement(_icon.default, {
          onClick: function onClick(event) {
            return _this2.onPassword();
          },
          className: passwordClassNames,
          name: "EyeOutlined",
          size: 20
        });
      } // 自定义后缀


      var appendNode;
      var inputStyle;

      if (append) {
        appendNode = /*#__PURE__*/_react.default.createElement("div", {
          style: {
            width: appendWidth
          },
          className: "".concat(componentPrefix, "__append")
        }, append);
        inputStyle = {
          paddingRight: appendWidth
        };
      } // 可清除


      var clearableNode;

      if (clearable && value !== null && value !== void 0 && value.length) {
        clearableNode = /*#__PURE__*/_react.default.createElement(_icon.default, {
          onClick: function onClick(event) {
            return _this2.onClearable(event);
          },
          className: "".concat(componentPrefix, "__icon"),
          name: "WrongOutlined",
          size: 20
        });
      } // 限制字数


      var limitNode;

      if (showLimit && maxLength >= 0) {
        limitNode = /*#__PURE__*/_react.default.createElement("div", {
          className: "".concat(componentPrefix, "__limit")
        }, value.length, "/", maxLength);
      } // 图标


      var iconNode;

      if (icon) {
        iconNode = /*#__PURE__*/_react.default.createElement(_icon.default, {
          onClick: function onClick(event) {
            return _this2.onIconClick(event);
          },
          className: "".concat(componentPrefix, "__icon"),
          name: icon,
          size: 16
        });
      } // 头部图标


      var prefixIconNode;

      if (prefixIcon) {
        prefixIconNode = /*#__PURE__*/_react.default.createElement(_icon.default, {
          onClick: function onClick(event) {
            return _this2.onPrefixIconClick(event);
          },
          className: "".concat(componentPrefix, "__prefix-icon"),
          name: prefixIcon,
          size: 16
        });
      }

      return /*#__PURE__*/_react.default.createElement("div", {
        style: style,
        className: componentClassNames,
        onClick: function onClick(event) {
          return _this2.onClick(event);
        }
      }, iconNode, limitNode, clearableNode, modeNode, prefixIconNode, /*#__PURE__*/_react.default.createElement("input", _extends({
        ref: "input"
      }, webProps, {
        disabled: disabled,
        maxLength: maxLength,
        value: value,
        style: inputStyle,
        placeholder: placeholder,
        className: "".concat(componentPrefix, "__inner"),
        type: this.state.type,
        onInput: function onInput(event) {
          return _this2.onInput(event);
        },
        onKeyDown: function onKeyDown(event) {
          return _this2.onKeyDown(event);
        },
        onFocus: function onFocus(event) {
          return _this2.onFocus(event);
        },
        onBlur: function onBlur(event) {
          return _this2.onBlur(event);
        },
        onCompositionStart: this.onCompositionStart,
        onCompositionEnd: this.onCompositionEnd
      })), appendNode);
    }
  }]);

  return WebLayout;
}(_react.Component);

exports.WebLayout = WebLayout;

_defineProperty(WebLayout, "contextType", _configContext.ConfigContext);